package com.huli.controller;

import com.huli.domain.Car;
import com.huli.domain.User;
import com.huli.param.*;
import com.huli.service.CarService;
import com.huli.service.impl.UserServiceImpl;
import com.huli.utils.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
public class UserController {

    @Autowired
    UserServiceImpl userService;

    @Autowired
    CarService carService;

    @ApiOperation(value = "用户注册")
    @PostMapping("/user/register")
    public Result register(UserRegisterParam registerParam) {
        userService.register(registerParam);
        return Result.successResult();
    }

    @ApiOperation(value = "用户登录")
    @PostMapping("/user/login")
    public Result login(UserLoginParam param) {
        String login = userService.login(param);
        return Result.successResult(login);
    }

    @ApiOperation(value = "通过用户id得到用户")
    @GetMapping("/user/{userId}")
    public Result getUser(@PathVariable("userId") int userId) {
        User user = userService.getUser(userId);
        return Result.successResult(user);
    }

    @ApiOperation(value = "用户抢车位")
    @PostMapping("/user/rent")
    public Result userRent(OrdersCreateParam param) {
        userService.userRent(param);
        return Result.successResult();
    }

    @ApiOperation(value = "//暂时弃用")
    @PostMapping("/user/recharge")
    public Result userRecharge() {
        List<User> users = userService.userRecharge();
        return Result.successResult(users);
    }

    @ApiOperation(value = "改变用户积分")
    @PutMapping("/user")
    public Result changeSource(int userId, int userSource) {
        userService.changeSource(userId, userSource);
        return Result.successResult();
    }

    @ApiOperation(value = "改变用户状态 此接口由计时器自动调用 改变信誉积分不足的用户状态")
    @DeleteMapping("/user")
    public Result changeStatus() {
        userService.changeStatus();
        return Result.successResult();
    }

    @ApiOperation(value = "通过邀请码获取用户")
    @GetMapping("/user/invite")
    public Result getUserByInvite(String userInvite) {
        User userByInvite = userService.getUserByInvite(userInvite);
        return Result.successResult(userByInvite);
    }

    @ApiOperation(value = "用户购买VIP")
    @PostMapping("/user/vip")
    public Result buyVip(int userId, int vipId) {
        userService.buyVip(userId, vipId);
        return Result.successResult();
    }

    @ApiOperation(value = "用户改变信息")
    @PutMapping("/user/info")
    public Result changeInfo(UserInfoParam userInfoParam) {
        userService.changeInfo(userInfoParam);
        return Result.successResult();
    }

    @ApiOperation(value = "改变用户的钱 此接口由其他接口调用")
    @PutMapping("/user/money")
    public Result changeMoney(int userId, BigDecimal userMoney) {
        userService.changeMoney(userId, userMoney);
        return Result.successResult();
    }

    @ApiOperation(value = "用户新增车辆")
    @PostMapping("/user/car")
    public Result newCar(UserNewCarParam param) {
        carService.newCar(param);
        return Result.successResult();
    }

    @ApiOperation(value = "获取该用户所有的车")
    @GetMapping("/user/car")
    public Result getCarList(int userId, int carCheck) {
        List<Car> carList = carService.getCarList(userId, carCheck);
        return Result.successResult(carList);
    }

    @ApiOperation(value = "通过车辆id获取用户")
    @GetMapping("/user/car/{carId}")
    public Result getUserByCarId(@PathVariable("carId") int carId) {
        User user = carService.getUserByCarId(carId);
        return Result.successResult(user);
    }

    @ApiOperation(value = "改变用户的VIP状态 根据计时器自动调用该接口")
    @DeleteMapping("/user/vip")
    public Result changeUserVip(){
        userService.changeVip();
        return Result.successResult();
    }
}
